<script>
    function deleteSelected(frm, qstr, itemName, actionFile)
    {
        qstr = (qstr != "") ? "&" + qstr : qstr;
        if (confirmdelcheckeditems(itemName, frm)) {
            frm.action = actionFile + ".php?action=muliDelete" + qstr;
            frm.submit();
        }
    }
    function EditSelected(frm, qstr, text, itemName)
    {
        qstr = (qstr != "") ? "&" + qstr : qstr;
        if (confirmEditcheckeditems(itemName, text, frm)) {
            frm.action = "index.php?view=edit" + qstr;
            frm.submit();

        }
    }
    $(document).ready(function() {
        $("#success").delay(3000);
        $("#success").fadeOut(1000);
        $("#simpleConfirm").confirm();
    });
</script>

<style>
    table tr:first-child{height:33px;text-align:center;}
    .message_contain{height:50px;margin:0;overflow:hidden;}
</style>
<?php
if (!defined('W_ROOT')) {
    exit();
}
$_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];
$pharmacy_id = $_SESSION['acc_id'];
$qstring = '';
$changeCate = 0;
$sql2 = '';
$sqlUserID = '';
$statusSql = '';
$rowsPerPage = isset($_GET['num']) ? $_GET['num'] : '10';
if ((isset($_GET['cboCategory']) && (int) $_GET['cboCategory'] > 0)) {
    $changeCate = $_GET['cboCategory'];
    $sql2 = " AND pic.category_id = $changeCate";
    $qstring = "catId=$changeCate";
}
if ((isset($_GET['cboUserID']) && (int) $_GET['cboUserID'] > 0)) {
    $ownerUser = $_GET['cboUserID'];
    $sqlUserID = " AND p.owner = $ownerUser";
}
if ((isset($_GET['status']) && (int) $_GET['status'] != 3)) {
    $status = $_GET['status'];
    $statusSql = " AND pip.status = $status";
}
$categoryList = buildCategoryOptions($changeCate);
$sql = "SELECT 
   					p.*,pip.*,pic.*, COUNT(pip.country_id) AS num_count 
   				FROM 
   				tblproduct AS p,tblproduct_in_pharmacy AS pip, tblproduct_in_category AS pic 
   				WHERE 
   					p.id = pip.product_id  
   				AND 
   					pic.product_id = pip.product_id
   					$sqlUserID
   					 $sql2 
   					 $statusSql
   				 GROUP BY 
   				 	pip.product_id 
   				 ORDER BY 
   				pip.pid DESC";
$result = mysql_query(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage);
$row_num = mysql_num_rows($result);
?>
<div class="breadcrumb">
    <a href="<?php echo base_url_admin; ?>"><?php echo $lang['HOME']; ?></a>
    ::<?php echo $lang['PRO_LIST']; ?>
</div>
<div class="box">
    <style>
        table tr:first-child{height:33px;text-align:left;}
    </style>
    <?php
    switch (@$_GET['msg']) {
        case 'add':
            echo '<div class="success">Product has been added|updated successfully</div>';
            break;
        case 'delete_success':
            echo '<div class="warning">Product has been deleted successfully</div>';
            break;
    }
    ?>
    <div class="heading">
        <h1><img src="../templates/image/product.png" alt="" />

            <?php echo $lang['PRO_LIST']; ?>

        </h1>
        <div class="buttons">
            <form action="<?php echo base_url_admin ?>/product/index.php" method="get" name="frmlist">

                <input class="button" type="button" name="btnadd" value="<?php echo $lang['ADD_NEW']; ?>" onClick="javascript:window.location.href = 'index.php?view=add&amp';
        return false;" />&nbsp;&nbsp;
                <input class="button" type="button" name="btndelete" id="deleteProduct" value="<?php echo $lang['MY_DEL']; ?>" onclick="multiDelete()" />
        </div>
        <?php
        if (isset($_POST['btndelete'])) {
            $i = 0;
            while (list($key, $val) = each($_POST['chkall'])) {
                $sql = "DELETE FROM tblproduct WHERE id = $val";
                mysql_query($sql);
                $i += mysql_affected_rows();
            }
        }
        ?>
    </div>
    <div class="content">
        <div class="row">
            <?php
            $mysqlUser = "SELECT first_name, last_name,id FROM tbluser WHERE status=1";
            $resultRows = mysql_query($mysqlUser);
            ?>
            <div style="float:left; padding-top:15px;">
                <select name="cboUserID">
                    <option value="0">---Select Users---</option>
                    <?php
                    while ($rowsUser = mysql_fetch_object($resultRows)) {
                        ?>
                        <option <?php echo (@$_GET['cboUserID'] == $rowsUser->id) ? "selected" : ""; ?> value="<?php echo $rowsUser->id; ?>"><?php echo $rowsUser->first_name . ' ' . $rowsUser->last_name; ?></option>
                    <?php } ?>
                </select>
                <select name="cboCategory" id="cboCategory" style="width:144px;">
                    <option value="0"><?php echo $lang['VIEW_BY_CAT']; ?></option>
                    <?php echo $categoryList; ?>
                </select> 
                <select name="status">
                    <option  value="3">Status</option>
                    <option  value="1">Publish</option>
                    <option  value="0">Unpublish</option>
                </select>
                <select class="right span4" name="num">
                    <option <?php echo($rowsPerPage == 10) ? ' selected="selected"' : ''; ?> value="10">Number of Item</option>
                    <option value="20"
                            <?php echo($rowsPerPage == 20) ? ' selected="selected"' : ''; ?>>20</option>
                    <option value="30"
                            <?php echo($rowsPerPage == 30) ? ' selected="selected"' : ''; ?>>30</option>
                    <option value="40"
                            <?php echo($rowsPerPage == 40) ? ' selected="selected"' : ''; ?>>40</option>
                    <option value="50"
                            <?php echo($rowsPerPage == 50) ? ' selected="selected"' : ''; ?>>50</option>
                    <option value="100"
                            <?php echo($rowsPerPage == 100) ? ' selected="selected"' : ''; ?>>100</option>
                    <option value="120"
                            <?php echo($rowsPerPage == 120) ? ' selected="selected"' : ''; ?>>120</option>
                </select>
                <input type="submit" name="btnFilter" value="Filter">
                <a href="<?php echo base_url_admin ?>/product/index.php" class="button">View All</a>
            </div>
            <div style="clear:both;">&nbsp;</div>
            <div class="box">
                <div class="title_admin" >
                    </form>
                    <form action="process.php?action=multiDelete" method="POST" name="frmlist" id="multidelete">
                        <table class="list">
                            <thead>
                                <tr>
                                    <td align="center" style="width: 20px;">
                                        <input type="checkbox" name="chkall" id="chkall" onClick="checkAll(this.form, this.checked)" style="cursor:pointer;"></td>
                                    <td align="center" style="width: 75px;"><?php echo $lang['IMG']; ?></td>
                                    <td align="left" style="padding-left:15px;"><?php echo $lang['PRO_NAME']; ?></td>
                                    <td style="width: 150px;"><?php echo $lang['CON_PRICE']; ?></th>
                                    <td style="width: 75px;"><?php echo $lang['STATUS']; ?></td>
                                    <td width="12%"><?php echo $lang['ACTION']; ?></td>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 1;
                                if (dbNumRows($result) > 0) {
                                    while ($row = mysql_fetch_assoc($result)) {
                                        extract($row);

                                        if ($i % 2) {
                                            $class = 'row1';
                                        } else {
                                            $class = 'row2';
                                        }

                                        $sql_img = "SELECT images FROM tblimage WHERE product_id = $id";
                                        $result_img = mysql_query($sql_img);
                                        $row_img = mysql_fetch_assoc($result_img);
                                        $pathImage = S_S_IMAGES_DIR;
                                        $pathImages = base_url . 'product/images/';
                                        if ($row_img['images'] && file_exists($pathImage . $row_img['images'])) {
                                            $thumbnail_w = $pathImages . 'thumb/' . $row_img['images'];
                                            $thumbnail_s = $pathImages . 'thumb/' . $row_img['images'];
                                        } else {
                                            $thumbnail_w = 'images/no-image-small.png';
                                            $thumbnail_s = 'images/no-image-small.png';
                                        }
                                        //Resample original image to display as Thumtail

                                        $country = mysql_query("SELECT COUNT(DISTINCT country_id) AS pid
		FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id);

                                        $price = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price ASC LIMIT 0,1");
                                        $myprice = mysql_fetch_assoc($price);
                                        $price1 = mysql_query("SELECT price FROM tblproduct_in_pharmacy WHERE product_id=" . $id . " AND pharmacy_id=" . $pharmacy_id . " ORDER BY price DESC LIMIT 0,1");
                                        $myprice1 = mysql_fetch_assoc($price1);
                                        ?>
                                        <tr>
                                            <td width="60" align="center">
                                                <input type="checkbox" name="id[]" value="<?php echo @$id; ?>"/>
                                            </td>
                                            <td width="100" align="center"><img src="<?php echo $thumbnail_w; ?>" width="75" height="75" border="0" /></td>
                                            <td align="left" style="padding-left:15px;">
                                                <strong style="color:#06C;"><?php echo stripslashes($product_name) ?></strong><br />
                                                <em style="color:#666;">( Made in 
                                                    <?php
                                                    if ($num_count > 1) {
                                                        echo $num_count . " Countries";
                                                    } else {
                                                        echo getCountryName($country_id);
                                                    }
                                                    ?>
                                                    )</em>
                                            </td>
                                            <td align="center"><strong style="color:#F60">
                                                    <?php
                                                    if ($myprice['price'] == $myprice1['price']) {
                                                        echo '$' . $myprice['price'];
                                                    } else {
                                                        echo '$' . $myprice['price'] . ' - $' . $myprice1['price'];
                                                    }
                                                    ?>
                                                </strong></td>
                                            <td align="center"><?php echo stateProduct($id, $pharmacy_id, $status) ?></td>
                                            <td align="center">
        <?php
        if ($owner == $pharmacy_id) {
            $edit = $id;
        } else {
            $edit = $id . '&owner=0';
        }
        ?>
                                                <div class="btn-group">
                                                    <a  href="<?php echo base_url_admin; ?>/product/index.php?view=edit&id=<?php echo $edit; ?>"><img width="20" src="../templates/image/Edit.gif" alt="<?php echo $lang['EDIT']; ?>" /></a>
                                                    <a  onclick="return ConfirmDelete('<?php echo base_url_admin; ?>/product/index.php?view=delete&id=<?php echo $id ?>')" href="javascript:;"><img width="20" src="../templates/image/trash.png" alt="<?php echo $lang['DELETE']; ?>" /></a>
                                                </div>
                                            </td>
                                        </tr>
        <?php
        $i++;
    }// end while
} else {
    ?>
                                    <tr>
                                        <td colspan="6" style="height:75px;" align="center"><strong>You don't have any product yet! Please add new product</strong></td>
                                    </tr>
                                    <?php
                                }
                                ?> 
                            </tbody>       
                        </table>
                </div>

            </div>
            <!--<div class="box">-->
            <div class="row">
                <div class="span7">
                    <div class="wp-pagenavi">
<?php echo $pagingLink; ?>
                        <div class="clear"></div>
                    </div>
                </div>

                </form>
            </div><!--<div class="box">-->


        </div><!--<div class="row-fluid">-->
    </div>
</div>
<!--<div class="row-fluid">-->




